﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using System;
using System.Diagnostics;
using MbUnit.Framework;
using WatiN.Core;
using DotNetNuke.Tests.UI.WatiN.Common;
using DotNetNuke.Tests.UI.WatiN.Utilities;
using WatiN.Core.DialogHandlers;
using DotNetNuke.Tests.UI.WatiN.Common.WatiNObjects;
using System.IO;
using System.Text.RegularExpressions;



namespace DotNetNuke.Tests.UI.WatiN.RegressionTests
{
    [TestFixture]


    public class FiveFourTests : RTBase
    {

        #region Test Setup and TearDown

        public bool LanguagePackInstalled = false;

        [SetUp]
        public void SetUp()
        {

        }

        [TearDown]
        public void TearDown()
        {
            if (LanguagePackInstalled)
            {
                WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
                LoginPage loginPage = new LoginPage(homePage);
                if (!homePage.RegisterLink.Text.Contains(TestUsers.Host.DisplayName))
                {
                    if (!homePage.RegisterLink.Text.Equals("Register"))
                    {
                        loginPage.LogoffUser();

                    }
                    loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
                }
                //Uninstall the language pack and delete the files
                ieInstance.GoTo(SiteURL + "/Host/Extensions.aspx");
                ExtensionsPage extensionsPage = new ExtensionsPage(homePage);
                extensionsPage.ExtensionPackageSelectList.Select("Core Language Pack");
                System.Threading.Thread.Sleep(2000);
                extensionsPage.GetExtensionUninstallButton(LocaleName).Click();
                System.Threading.Thread.Sleep(2000);
                extensionsPage.DeleteExtensionFilesCheckbox.Checked = true;
                ConfirmDialogHandler dialog = new ConfirmDialogHandler();
                using (new UseDialogOnce(ieInstance.DialogWatcher, dialog))
                {
                    extensionsPage.UninstallPackageLink.ClickNoWait();
                    dialog.WaitUntilExists();
                    dialog.OKButton.Click();
                }
                ieInstance.WaitForComplete();
                LanguagePackInstalled = false;
            }
            TearDownTest();
        }

        #endregion

        #region Tests

        [Test]
        public void DNN_10874_Potential_Database_Optimizations()
        {
            SetUpTestForInstall();
            RunAutoInstall();

            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);

            //Act
            ieInstance.GoTo(SiteURL + "/Host/SQL.aspx");
            HostSQLPage sqlPage = new HostSQLPage(homePage);
            sqlPage.SQLQueryTextField.Value = "sp_helptext GetTabPermissionsByPortal;";
            sqlPage.ExecuteSQLLink.Click();

            //Assert 
            Assert.IsTrue(sqlPage.QueryResults.InnerHtml.Contains("CREATE procedure dbo.GetTabPermissionsByPortal"));
            Assert.IsTrue(sqlPage.QueryResults.InnerHtml.Contains("@PortalID int"));
            Assert.IsTrue(sqlPage.QueryResults.InnerHtml.Contains("BEGIN"));
            Assert.IsTrue(sqlPage.QueryResults.InnerHtml.Contains("SELECT *"));
            Assert.IsTrue(sqlPage.QueryResults.InnerHtml.Contains("FROM dbo.vw_TabPermissions"));
            Assert.IsTrue(sqlPage.QueryResults.InnerHtml.Contains("WHERE PortalID = @PortalID"));
            Assert.IsTrue(sqlPage.QueryResults.InnerHtml.Contains("END"));
            Assert.IsTrue(sqlPage.QueryResults.InnerHtml.Contains("ELSE"));
            Assert.IsTrue(sqlPage.QueryResults.InnerHtml.Contains("BEGIN"));
            Assert.IsTrue(sqlPage.QueryResults.InnerHtml.Contains("SELECT *"));
            Assert.IsTrue(sqlPage.QueryResults.InnerHtml.Contains("FROM dbo.vw_TabPermissions"));
            Assert.IsTrue(sqlPage.QueryResults.InnerHtml.Contains("WHERE PortalID IS NULL"));
            Assert.IsTrue(sqlPage.QueryResults.InnerHtml.Contains("END"));
        }

        [Test]
        public void DNN_12032_Export_Import_Page_Broken()
        {
            SetUpTestForInstall();
            RunAutoInstall();

            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);

            ieInstance.GoTo(SiteURL + "/Home.aspx");
            ExportImportPage exportImportPage = new ExportImportPage(homePage);
            ControlPanel controlPanel = null;
            RibbonBar ribbonBar = null;
            bool usesRibbonBar = false;

            //Click the export page link on the control panel
            if (homePage.ControlPanelElement.Id.Contains(homePage.RibbonBarID))
            {
                ribbonBar = new RibbonBar(homePage);
                ribbonBar.CurrentPageTabLink.Click();
                ribbonBar.ExportPageLink.Click();
                usesRibbonBar = true;
            }
            else
            {
                controlPanel = new ControlPanel(homePage);
                controlPanel.ExportPageLink.Click();
            }
            exportImportPage.TemplateNameField.Value = "Test1";
            exportImportPage.TemplateDescriptionField.Value = "A test template.";
            exportImportPage.IncludeContentCheckbox.Checked = true;
            exportImportPage.ExportLink.Click();

            Assert.IsTrue(exportImportPage.ExportMessageSpan.InnerHtml.Contains("The Page Template Has Been Created to"));
            Assert.IsTrue(File.Exists(PhysicalPath + @"\Portals\0\Templates\Test1.page.template"));

            System.Threading.Thread.Sleep(500);
            ieInstance.GoTo(SiteURL + "/Home.aspx");
            if (usesRibbonBar)
            {
                ribbonBar.ImportPageLink.Click();
            }
            else
            {
                ribbonBar.ImportPageLink.Click();
            }

            exportImportPage.TemplateSelectList.Select("Test1");
            exportImportPage.CreateNewPageRadioButton.Checked = true;
            exportImportPage.PageNameField.Value = "Test Import";
            exportImportPage.ParentPageSelectList.Select("<None Specified>");
            exportImportPage.InsertPageSelectList.Select("Home");
            exportImportPage.ViewImportedPageRadioButton.Checked = true;
            exportImportPage.ImportPageLink.Click();

            SiteMenu menu = new SiteMenu(homePage);
            Assert.IsTrue(menu.BreadCrumbLink.Text.Equals("Test Import"));
            Assert.IsTrue(menu.MainMenu.InnerHtml.Contains("Test Import"));
            Assert.IsTrue(menu.MainMenu.InnerHtml.Contains("Home"));
        }

        [Test]
        public void DNN_12129_Module_DropDown_Has_Incorrect_Admin_ContentList_Listing()
        {
            SetUpTestForInstall();
            RunAutoInstall();

            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            
            ieInstance.GoTo(SiteURL + "/tabid/16/portalid/0/Default.aspx");
            SelectRibbonBarAsControlPanelStyle(TestUsers.Host.Email, homePage);

            //Act
            RibbonBar ribbonBar = new RibbonBar(homePage);
            ribbonBar.ModuleComboBox.Click();
            Div comboBoxContents = ieInstance.Div(Find.ById("dnn_RibbonBar.ascx_AddMod_ModuleLst_DropDown"));

            //Assert 
            Assert.IsTrue(comboBoxContents.InnerHtml.Contains("ContentList"));
            Assert.IsFalse(comboBoxContents.InnerHtml.Contains("Admin.ContentList"));
        }

        [Test]
        public void DNN_12149_View_Profile_Module_Corrupts_Page()
        {
            SetUpTestForInstall();
            RunAutoInstall();
            
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            String pageName = "Test Page";
            ControlPanel controlPanel = null;
            RibbonBar ribbonBar = null;
            bool usesRibbonBar = false;

            //Add a page to the site
            if (homePage.ControlPanelElement.Id.Contains(homePage.RibbonBarID))
            {
                ribbonBar = new RibbonBar(homePage);
                ribbonBar.NewPageLink.Click();
                usesRibbonBar = true;
            }
            else
            {
                controlPanel = new ControlPanel(homePage);
                controlPanel.AddPageLink.Click();
            }
            PageSettingsPage newPage = new PageSettingsPage(homePage);
            newPage.AddPageSelectTemplate(pageName, "None Specified");

            //Act 
            if (!usesRibbonBar)
            {
                controlPanel.AddModuleToPage("", "ViewProfile"); 
            }
            else
            {
                ribbonBar.AddModuleToPage("", "ViewProfile", "All Categories");
            }

            //Assert 
            System.Threading.Thread.Sleep(2000);
            Div moduleContent = ieInstance.Div(Find.ById(s => s.EndsWith("ModuleContent")));
            Assert.IsTrue(homePage.ModuleTitleSpan.InnerHtml.Contains("ViewProfile"));
            Assert.IsTrue(moduleContent.InnerHtml.Contains("This module is only intended for use on a page which is defined as the profile page for the portal."));
        }

        [Test]
        public void DNN_12150_Dummy_Module()
        {
            SetUpTestForInstall();
            RunAutoInstall();

            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);

            ieInstance.GoTo(SiteURL + "/tabid/16/portalid/0/Default.aspx");
            SelectRibbonBarAsControlPanelStyle(TestUsers.Host.Email, homePage);

            String pageName = "Test Page";
            RibbonBar ribbonBar = null;

            //Add a page to the site
            ribbonBar = new RibbonBar(homePage);
            ribbonBar.NewPageLink.Click();
            PageSettingsPage newPage = new PageSettingsPage(homePage);
            newPage.AddPageSelectTemplate(pageName, "None Specified");

            //Act 
            ribbonBar.AddModuleToPage("Test Content List", "ContentList", "All Categories");

            //Assert 
            System.Threading.Thread.Sleep(2000);
            Div moduleContent = ieInstance.Div(Find.ById(s => s.EndsWith("ModuleContent")));
            Assert.IsTrue(homePage.ModuleTitleSpan.InnerHtml.Contains("Content List"));
            Assert.IsTrue(moduleContent.InnerHtml.Contains("This module is hidden to regular users if there is no"));
        }

        [Test]
        public void DNN_12189_Site_Settings_locale_Settings_Cannot_Be_Changed_from_English()
        {
            SetUpTestForInstall();
            RunAutoInstall();

            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);

            //Add a Language
            ieInstance.GoTo(SiteURL + "/Admin/Extensions.aspx");
            ExtensionsPage extensions = new ExtensionsPage(homePage);
            extensions.InstallExtension(LanguagePath);
            LanguagePackInstalled = true;

            //Change the site Settings
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            LanguagePage languagePage = new LanguagePage(homePage);
            languagePage.SelectAndUpdateNewDefaultLanguage(LocaleName, "rcbItem ");

            //Act 
            ieInstance.GoTo(SiteURL + "/Admin/SiteSettings.aspx");
            SiteSettingsPage settingsPage = new SiteSettingsPage(homePage);
            settingsPage.SiteTitleField.Value = "New Language Site";
            settingsPage.CopyrightField.Value = "New Language Copyright";
            settingsPage.UpdateLink.Click();
            System.Threading.Thread.Sleep(1000);

            //Assert
            ieInstance.GoTo(SiteURL + "/Home.aspx");
            System.Threading.Thread.Sleep(1000);
            Assert.IsTrue(ieInstance.Title.Contains("New Language Site"));
            Assert.IsTrue(homePage.CopyrightSpan.InnerHtml.Contains("New Language Copyright"));
        }

        [Test]
        public void DNN_12004_PM_Mails_Sent_Externally_Have_Formatting_Tags_in_Plain_Txt_Msg()
        {
            //Arrange
            SetUpTestForInstall();
            RunAutoInstall();

            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);

            //Delete any files in the test EmailAddress folder
            homePage.DeleteFilesInTestEmails();
            //Edit the web.config file so that Emails are sent to a local folder
            WebConfigManager.UpdateConfig(PhysicalPath);

           
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);

            ieInstance.GoTo(SiteURL + "/Host/Schedule.aspx");
            SchedulePage schedular = new SchedulePage(homePage);
            schedular.GetScheduleItemEditButton("Messaging Dispatch").Click();
            schedular.TimeLapseTextField.Value = "30";
            schedular.TimeLapseMeasurementSelectList.Select("Seconds");
            schedular.UpdateLink.Click();
            System.Threading.Thread.Sleep(2000);

            //Configure EmailAddress for the site
            ieInstance.GoTo(SiteURL + "/tabid/16/portalid/0/Default.aspx");
            HostSettingsPage hostSettings = new HostSettingsPage(homePage);
            hostSettings.SetSchedularToTimerMethod("support@dnn.com");
            System.Threading.Thread.Sleep(1000);
            hostSettings.SetSMTPSettings("test@dnn.com", "localhost");

            //Add Users
            UserPage userPage = new UserPage(homePage);
            ieInstance.GoTo(SiteURL + "/Admin/UserAccounts.aspx");
            userPage.AddUser(TestUsers.User1.UserName, TestUsers.User1.Password, TestUsers.User1.FirstName, TestUsers.User1.LastName, TestUsers.User1.EmailAddress, TestUsers.User1.DisplayName);
            System.Threading.Thread.Sleep(2000);
            userPage.AddUser(TestUsers.User2.UserName, TestUsers.User2.Password, TestUsers.User2.FirstName, TestUsers.User2.LastName, TestUsers.User2.EmailAddress, TestUsers.User2.DisplayName);
            System.Threading.Thread.Sleep(2000);

            //Act 
            //Log in as one of the users
            ieInstance.GoTo(SiteURL + "/Home.aspx");
            loginPage.LogoffUser();
            loginPage.LoginUser(TestUsers.User1.UserName, TestUsers.User1.Password);

            //Send a message
            homePage.RegisterLink.Click();
            MessagingModule messaging = new MessagingModule(homePage);
            messaging.SendMsgToUser(TestUsers.User2.UserName, "Test Msg", "<p><b> This is important Text! </b></p>");
        
            //Assert
            System.Threading.Thread.Sleep(90000);
            String emailFileName = null;
            String emailContent = null;
            int emailFilePosition = 0;
            for (int i = 0; i < Directory.GetFiles(TestEmailPath).Length; i++)
            {
                emailFileName = Directory.GetFiles(TestEmailPath)[i];
                emailContent = File.ReadAllText(emailFileName);
                if (emailContent.Contains("X-Receiver: " + TestUsers.User2.EmailAddress) && emailContent.Contains("Sender: " + TestUsers.User1.EmailAddress))
                {
                    emailFilePosition = i;
                }
            }

            emailFileName = Directory.GetFiles(TestEmailPath)[emailFilePosition];
            emailContent = File.ReadAllText(emailFileName);
            emailContent.Replace(Environment.NewLine, " ");
            string contentLineRegex = "(?<=base64).*$";
            Match match = Regex.Match(@emailContent, contentLineRegex, RegexOptions.Singleline);
            Assert.IsTrue(match.Success, "Could not find content Line! Looking in file: " + emailFileName);
            emailContent = match.Value;

            byte[] b = System.Convert.FromBase64String(emailContent);
            String decodedEmailContent = System.Text.ASCIIEncoding.ASCII.GetString(b);
            Assert.IsTrue(decodedEmailContent.CompareTo("<b>This is important Text! </b>") == 0, "Looking at string: " + decodedEmailContent);
        }

        [Test]
        public void DNN_12122_Remove_Admin_Only_Border_Behavior_Based_On_Menu_Location()
        {
            SetUpTestForInstall();
            RunAutoInstall();

            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            ieInstance.GoTo(SiteURL + "/tabid/16/portalid/0/Default.aspx");
            SelectRibbonBarAsControlPanelStyle(TestUsers.Host.Email, homePage);

            String pageName = "Test Page";
            RibbonBar ribbonBar = null;

            //Add a page to the site
            ieInstance.GoTo(SiteURL + "/Home.aspx");
            ribbonBar = new RibbonBar(homePage);
            ribbonBar.NewPageLink.Click();
            PageSettingsPage newPage = new PageSettingsPage(homePage);
            newPage.AddPageSelectTemplate(pageName, "None Specified");

            //Add a module to the page 
            ribbonBar.AddHTMLModuleToPage("Test Module", "HTML");

            //Assert 
            System.Threading.Thread.Sleep(1000);
            Assert.IsTrue(homePage.ContentPane.InnerHtml.Contains("Visible By Administrators Only"));
            ModulePage module = new ModulePage(homePage);

            //Act
            module.GetModuleSettingsImage(0).Click();
            System.Threading.Thread.Sleep(1000);
            module.AdvancedSettingsExpandButton.Click();
            module.DisplayAdminBorderCheckbox.Checked = true;
            module.UpdateLink.Click();
            System.Threading.Thread.Sleep(1000);

            //Assert 
            Assert.IsFalse(homePage.ContentPane.InnerHtml.Contains("Visible By Administrators Only"));

            //Act
            module.GetModuleSettingsImage(0).Click();
            System.Threading.Thread.Sleep(1000);
            module.AdvancedSettingsExpandButton.Click();
            module.DisplayAdminBorderCheckbox.Checked = false;
            module.UpdateLink.Click();
            System.Threading.Thread.Sleep(1000);

            //Assert
            Assert.IsTrue(homePage.ContentPane.InnerHtml.Contains("Visible By Administrators Only"));
        }

        [Test]
        public void DNN_12207_Telerik_Editor_Folder_Creation_Folder_Listing_Not_Working()
        {
            SetUpTestForInstall();
            RunAutoInstall();

            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            ieInstance.GoTo(SiteURL + "/tabid/16/portalid/0/Default.aspx");
            SelectRibbonBarAsControlPanelStyle(TestUsers.Host.Email, homePage);

            String pageName = "Test Page";
            RibbonBar ribbonBar = null;

            //Add a page to the site
            ieInstance.GoTo(SiteURL + "/Home.aspx");
            ribbonBar = new RibbonBar(homePage);
            ribbonBar.NewPageLink.Click();
            PageSettingsPage newPage = new PageSettingsPage(homePage);
            newPage.AddPageSelectTemplate(pageName, "None Specified");

            //Add a module to the page 
            ribbonBar.AddHTMLModuleToPage("Test Module", "HTML");

            HTMLModule module = new HTMLModule(homePage);
            module.EditModuleContentLink.Click();

            //Add a folder via the Telerik File Manager
            module.DocumentManagerSpan.Click();
            System.Threading.Thread.Sleep(2000);
            module.TelerikEditor.TelerikNewFolderIcon.Click();
            System.Threading.Thread.Sleep(1000);
            module.TelerikEditor.TelerikNewFolderNameField.Value = "Test Folder";
            module.TelerikEditor.TelerikCreateNewFolderLink.Click();
            System.Threading.Thread.Sleep(2000);

            //Assert
            Assert.IsTrue(module.TelerikEditor.TelerikFolderDialog.InnerHtml.Contains("Test Folder"));
            module.TelerikEditor.TelerikCancelButton.Click();
            System.Threading.Thread.Sleep(500);

            //Add a folder via the File Manager page
            ieInstance.GoTo(SiteURL + "/Admin/FileManager.aspx");
            FileManagerPage fileManager = new FileManagerPage(homePage);
            fileManager.FolderExpandImage.Click();
            fileManager.FolderTypeSelectList.Select("Standard - File System");
            fileManager.FolderNameTextField.Value = "Second Folder";
            fileManager.AddFolderButton.Click();
            Span newFolderSpan = fileManager.FoldersSpan.Span(Find.ByTitle("Second Folder"));

            //Assert
            Assert.IsTrue(newFolderSpan.Exists);
            Assert.IsTrue(fileManager.CurrentFolderSpan.InnerHtml.Contains(@"Portal Root\Second Folder"));

            ieInstance.GoTo(SiteURL + "/TestPage.aspx");
            module.EditModuleContentLink.Click();
            module.DocumentManagerSpan.Click();
            System.Threading.Thread.Sleep(2000);

            //Assert
            Assert.IsTrue(module.TelerikEditor.TelerikFolderDialog.InnerHtml.Contains("Test Folder"));
            Assert.IsTrue(module.TelerikEditor.TelerikFolderDialog.InnerHtml.Contains("Second Folder"));

            module.TelerikEditor.TelerikCancelButton.Click();
            System.Threading.Thread.Sleep(500);


            //Clean Up the Folders
            Directory.Delete(PhysicalPath + @"\Portals\0\Test Folder", true);
            Directory.Delete(PhysicalPath + @"\Portals\0\Second Folder", true);
        }

        [Test]
        public void DNN_12221_User_Registration_Admin_Massages_None_Being_Sent_To_Administrator_for_New_Users()
        {
            SetUpTestForInstall();
            RunAutoInstall();

            //Arrange
            //Edit the web.config file so that Emails are sent to a local folder
            WebConfigManager.UpdateConfig(PhysicalPath);

            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            ieInstance.GoTo(SiteURL + "/tabid/16/portalid/0/Default.aspx");
            SelectRibbonBarAsControlPanelStyle(TestUsers.Host.Email, homePage);

            HostSettingsPage settingsPage = new HostSettingsPage(homePage);
            settingsPage.SetSMTPSettings("test@dnn.com", "localhost");
            
            //Delete any files in the test EmailAddress folder
            homePage.DeleteFilesInTestEmails();
            
            //Act 
            ieInstance.GoTo(SiteURL + "/Home.aspx");
            loginPage.LogoffUser();
            loginPage.RegisterLink.Click();
            UserPage userPage = new UserPage(homePage);

            //Register a user
            userPage.UserNameField.Value = TestUsers.Register.UserName;
            userPage.FirstNameField.Value = TestUsers.Register.FirstName;
            userPage.LastNameField.Value = TestUsers.Register.LastName;
            userPage.DisplayNameField.Value = TestUsers.Register.DisplayName;
            userPage.EmailField.Value = TestUsers.Register.EmailAddress;
            userPage.PasswordField.Value = TestUsers.Register.Password;
            userPage.ConfirmPasswordField.Value = TestUsers.Register.Password;
            userPage.RegisterButton.Click();
            System.Threading.Thread.Sleep(1000);

            //Add a user
            loginPage.LogoffUser();
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            ieInstance.GoTo(SiteURL + "/Admin/UserAccounts.aspx");
            userPage.AddUser(TestUsers.User1.UserName, TestUsers.User1.Password, TestUsers.User1.FirstName, TestUsers.User1.LastName, TestUsers.User1.EmailAddress, TestUsers.User1.DisplayName);

            //Assert 
            Assert.IsTrue(Directory.GetFiles(TestEmailPath).Length > 0);
            System.Threading.Thread.Sleep(2000);
            String emailFileName = null;
            string emailContent = null;
            int[] adminPositions = new int[4];
            int adminIndex = 0;
            for (int i = 0; i < Directory.GetFiles(TestEmailPath).Length; i++)
            {
                emailFileName = Directory.GetFiles(TestEmailPath)[i];
               emailContent = File.ReadAllText(emailFileName);
                if (emailContent.Contains("X-Receiver: " + TestUsers.Admin.Email))
                {
                    //Take note of the position in the folder of the admin EmailAddress
                    adminPositions[adminIndex] = i;
                    adminIndex++;
                }
            }
            int seenZero = 0;
            byte[] b;
            string decodedEmailContent;
            foreach (int i in adminPositions)
            {
                if (i == 0)
                {
                    seenZero++;
                    if (seenZero > 1)
                    {
                        //move to the next item in the list
                        continue;
                    }
                }
                //Decode and View the file
                emailFileName = Directory.GetFiles(TestEmailPath)[i];
                emailContent = File.ReadAllText(emailFileName);
                b = System.Convert.FromBase64String(emailContent);
                decodedEmailContent = System.Text.ASCIIEncoding.ASCII.GetString(b);

            }

            //Delete any files in the test EmailAddress folder
            homePage.DeleteFilesInTestEmails();
        }

        [Test]
        public void DNN_12222_Enabling_A_New_Default_Language_Breaks_Site()
        {
            SetUpTestForInstall();
            RunAutoInstall();

            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);

            //Add a Language
            ieInstance.GoTo(SiteURL + "/Admin/Extensions.aspx");
            ExtensionsPage extensions = new ExtensionsPage(homePage);
            extensions.InstallExtension(LanguagePath);
            LanguagePackInstalled = true;

            //Act 
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            LanguagePage languagePage = new LanguagePage(homePage);
            languagePage.SelectAndUpdateNewDefaultLanguage(LocaleName, "rcbItem ");

            //Assert
            //Assert.IsFalse(homePage.PageContainsErrors());
            ieInstance.Image(Find.ByAlt(LanguageCode)).Click();
            //Assert.IsFalse(homePage.PageContainsErrors());
        }

        [Test]
        public void DNN_12242_Community_Gets_Access_To_KB_Articles()
        {
            if (VersionType.Equals("Professional"))
            {
                Assert.IsTrue(true);
                return;
            }

            SetUpTestForInstall();
            RunAutoInstall();

            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);

            //Act 
            RibbonBar ribbonBar = new RibbonBar(homePage);
            ribbonBar.HostTabLink.Click();

            //Assert
            System.Threading.Thread.Sleep(5000);
            Assert.IsFalse(ribbonBar.SupportLink.Exists);
        }

        [Test]
        public void DNN_12264_System_Mails_Stored_In_Cleartext_In_User_Messaging()
        {
            SetUpTestForInstall();
            RunAutoInstall();
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            
            //Delete any files in the test EmailAddress folder
            homePage.DeleteFilesInTestEmails();

            //Edit the web.config file so that Emails are sent to a local folder
            WebConfigManager.UpdateConfig(PhysicalPath);

            //Arrange
           
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);

            //Configure SMTP
            ieInstance.GoTo(SiteURL + "/tabid/16/portalid/0/Default.aspx");
            HostSettingsPage hostSettings = new HostSettingsPage(homePage);
            hostSettings.SetSMTPSettings("test@dnn.com", "localhost");

            UserPage userPage = new UserPage(homePage);
            ieInstance.GoTo(SiteURL + "/Admin/UserAccounts.aspx");
            userPage.AddUser(TestUsers.User1.UserName, TestUsers.User1.Password, TestUsers.User1.FirstName, TestUsers.User1.LastName, TestUsers.User1.EmailAddress, TestUsers.User1.DisplayName);
            
            //Act
            ieInstance.GoTo(SiteURL + "/Home.aspx");
            loginPage.LogoffUser();
            loginPage.LoginLink.Click();
            loginPage.ForgotPasswordLink.Click();

            loginPage.ForgotPasswordUserNameField.Value = TestUsers.User1.UserName;
            System.Threading.Thread.Sleep(1000);
            loginPage.SendPassworkLink.Click();

            //Assert 
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            ieInstance.GoTo(SiteURL + "/Host/SQL.aspx");

            HostSQLPage sql = new HostSQLPage(homePage);
            sql.SQLQueryTextField.Value = "SELECT * FROM [dbo].[Messaging_Messages]";
            sql.ExecuteSQLLink.Click();

            System.Threading.Thread.Sleep(1000);
            Assert.IsTrue(sql.QueryResults.InnerHtml.Contains("The query did not return any data"));
        }

        [Test]
        public void DNN_12139_Errors_Hydrating_Portal_Info()
        {
            SetUpTestForInstall();
            RunAutoInstall();

            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);

            //Act 
            ieInstance.GoTo(SiteURL + "/Admin/LogViewer.aspx");
            EventViewerPage eventViewer = new EventViewerPage(homePage);
            eventViewer.RecordsPerPageSelectList.Select("250");
            System.Threading.Thread.Sleep(2000);

            //Assert 
            Assert.IsFalse(eventViewer.LogViewerTable.InnerHtml.Contains("General Exception"));
        }

        [Test]
        public void DNN_12084_Site_Settings_Unavailable()
        {
            SetUpTestForInstall();
            RunAutoInstall();

            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);

            //Act 
            ieInstance.GoTo(SiteURL + "/Admin/SiteSettings.aspx");
            SiteSettingsPage settings = new SiteSettingsPage(homePage);
            settings.AdvancedSettingsButton.Click();
            settings.HostSettingsExpandButton.Click();
            settings.PageQuotaField.Value = "30";
            settings.UserQuotaField.Value = "30";
            settings.UpdateLink.Click();
            System.Threading.Thread.Sleep(2000);

            //Assert 
            Assert.IsFalse(homePage.ContentPane.Image(Find.ByTitle("Error")).Exists);
            ieInstance.GoTo(SiteURL + "/Admin/LogViewer.aspx");
            EventViewerPage eventViewer = new EventViewerPage(homePage);
            Assert.IsFalse(eventViewer.LogViewerTable.InnerHtml.Contains("General Exception"));
        }

        [Test]
        public void DNN_12088_HTML_Module_Not_Installed_In_Application()
        {
            SetUpTestForInstall();
            RunAutoInstall();

            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            String pageName = "Test Page";
            ControlPanel controlPanel = null;
            RibbonBar ribbonBar = null;

            //Act
            if (homePage.ControlPanelElement.Id.Contains(homePage.RibbonBarID))
            {
                ribbonBar = new RibbonBar(homePage);
                ribbonBar.NewPageLink.Click();
            }
            else
            {
                controlPanel = new ControlPanel(homePage);
                controlPanel.AddPageLink.Click();
            }
            PageSettingsPage newPage = new PageSettingsPage(homePage);
            newPage.AddPage(pageName);

            //Assert 
            Assert.IsTrue(homePage.ContentPane.InnerHtml.Contains("Enter Title"));
            HTMLModule module = new HTMLModule(homePage);
            Assert.IsTrue(module.HTMLContentDiv.Exists);
        }

        [Test]
        public void DNN_12181_Port_Telerik_RadEditor_Provider_PE_to_CE_Clean_Installation()
        {
            SetUpTestForInstall();
            RunAutoInstall();

            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            String pageName = "Test Page";
            ControlPanel controlPanel = null;
            RibbonBar ribbonBar = null;

            //Act
            if (homePage.ControlPanelElement.Id.Contains(homePage.RibbonBarID))
            {
                ribbonBar = new RibbonBar(homePage);
                ribbonBar.NewPageLink.Click();
            }
            else
            {
                controlPanel = new ControlPanel(homePage);
                controlPanel.AddPageLink.Click();
            }
            PageSettingsPage newPage = new PageSettingsPage(homePage);
            newPage.AddPage(pageName);

            ModulePage modulePage = new ModulePage(homePage);
            modulePage.EditModuleContentLink.Click();

            //Assert
            //Assert.IsTrue(homePage.TelerikEditorScriptElement.Exists);
            Assert.IsTrue(false);
        }

        [Test]
        public void DNN_12179_Cant_Upgrade_To_DNN_540()
        {
            //Arrange
            SetUpgradeVersionAndType("531", "1");
            SetUpSiteForUpgrade();
            ieInstance.GoTo(SiteURL + "/LogOff.aspx");

            //Shut down Ie
            WatiNUtil.CloseIEInstance(ieInstance);

            //Act 
            RunUpgrade();

            //Assert
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            Assert.IsTrue(homePage.LogoLink.Exists);
            Assert.IsTrue(homePage.LoginLink.Exists);
            Assert.IsTrue(homePage.RegisterLink.Exists);

            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            ieInstance.GoTo(SiteURL + "/Admin/LogViewer.aspx");
            EventViewerPage eventViewer = new EventViewerPage(homePage);
            eventViewer.RecordsPerPageSelectList.Select("50");
            System.Threading.Thread.Sleep(2000);
            Assert.IsFalse(eventViewer.LogViewerTable.InnerHtml.Contains("General Exception"));
        }

        [Test]
        public void DNN_12182_Upgrade_510_540_Fails_With_Stored_Procedure_GetPortalDefaultLanguage()
        {
            //Arrange
            SetUpgradeVersionAndType("510", "1");
            SetUpSiteForUpgrade();            

            //Add the XML module
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);

            ExtensionsPage extensions = new ExtensionsPage(homePage);
            ieInstance.GoTo(SiteURL + "/Admin/Extensions.aspx");
            extensions.InstallExtension(TestFilesPath + @"\XML_04.03.05_Install.zip");

            //Shut down Ie
            ieInstance.GoTo(SiteURL + "/LogOff.aspx");
            WatiNUtil.CloseIEInstance(ieInstance);

            //Act 
            RunUpgrade();

            //Assert
            homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            loginPage = new LoginPage(homePage);
            Assert.IsTrue(homePage.LoginLink.Exists);
            Assert.IsTrue(homePage.RegisterLink.Exists);
          }

        [Test]
        public void DNN_11700_Updating_Site_Settings_Does_Not_Work_In_Some_Cases()
        {
            //Arrange
            SetUpgradeVersionAndType("510", "1");
            SetUpSiteForUpgrade();

            //Add a Language
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);

            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            LanguagePage language = new LanguagePage(homePage);
            language.AddNewLanguageLink.Click();
            language.LanguageSelect.Select(LocaleName);
            language.FallbackLanguageSelect.Select("English (United States)");
            language.EnableNewLanguageCheckbox.Checked = true;
            language.UpdateLink.Click();

            //Change the default language for the site
            ieInstance.GoTo(SiteURL + "/Admin/SiteSettings.aspx");
            SiteSettingsPage settings = new SiteSettingsPage(homePage);

            settings.AdvancedSettingsButton.Click();
            settings.OtherSettingsExpandImage.Click();
            settings.DefaultLocaleSelectList.Select(LocaleName);
            settings.UpdateLink.Click();

            ieInstance.GoTo(SiteURL + "/Home.aspx");
            loginPage.LogoffUser();

            //Shut down Ie
            ieInstance.GoTo(SiteURL + "/LogOff.aspx");
            WatiNUtil.CloseIEInstance(ieInstance);

            //Act 
            RunUpgrade();

            //Assert
            homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            loginPage = new LoginPage(homePage);
            Assert.IsTrue(homePage.LoginLink.Exists);
            Assert.IsTrue(homePage.RegisterLink.Exists);
        }


        #endregion

        


        private void SelectRibbonBarAsControlPanelStyle(string hostEmail, WatiNBase homePage)
        {
            HostSettingsPage hostSettings = new HostSettingsPage(homePage);
            hostSettings.HostEmailTextField.Value = hostEmail;
            hostSettings.OtherSettingsExpandButton.Click();
            hostSettings.ControlPanelStyleSelectList.Select("RIBBONBAR");
            hostSettings.UpdateLink.Click();
            System.Threading.Thread.Sleep(1000);
        }


    }
}
